<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>MongoDB安装及使用 | 欢迎来到小张的博客</title><meta name="keywords" content="数据库"><meta name="author" content="Z_SiTong"><meta name="copyright" content="Z_SiTong"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="MongoDB安装MongoDB安装将MongoDB服务下载下来 安装到自己的电脑 MongoDB服务下载并安装 进入https:&#x2F;&#x2F;www.mongodb.com&#x2F;try&#x2F;download&#x2F;community  这个链接之下，会看到如下界面（一定要选择windows）    下载完成后，会得到一个后缀为.msi的文件，双击进行安装即可  以下是安装图解（要注意，除以下安装图解之外，全是next）">
<meta property="og:type" content="article">
<meta property="og:title" content="MongoDB安装及使用">
<meta property="og:url" content="https://zhang-sitong.gitee.io/blogs/lz_17/index.html">
<meta property="og:site_name" content="欢迎来到小张的博客">
<meta property="og:description" content="MongoDB安装MongoDB安装将MongoDB服务下载下来 安装到自己的电脑 MongoDB服务下载并安装 进入https:&#x2F;&#x2F;www.mongodb.com&#x2F;try&#x2F;download&#x2F;community  这个链接之下，会看到如下界面（一定要选择windows）    下载完成后，会得到一个后缀为.msi的文件，双击进行安装即可  以下是安装图解（要注意，除以下安装图解之外，全是next）">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://i.loli.net/2021/03/17/7x6uAiswPjbJknS.jpg">
<meta property="article:published_time" content="2019-03-18T11:16:58.000Z">
<meta property="article:modified_time" content="2021-03-23T01:16:13.118Z">
<meta property="article:author" content="Z_SiTong">
<meta property="article:tag" content="数据库">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2021/03/17/7x6uAiswPjbJknS.jpg"><link rel="shortcut icon" href="/blogs/img/favicon.png"><link rel="canonical" href="https://zhang-sitong.gitee.io/blogs/lz_17/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blogs/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blogs/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
};

const saveToLocal = {
  // ttl 單位是 天
  set: function setWithExpiry(key, value, ttl) {
    if (ttl === 0) return
    const now = new Date()
    const expiryDay = ttl * 86400000
    const item = {
      value: value,
      expiry: now.getTime() + expiryDay,
    }
    localStorage.setItem(key, JSON.stringify(item))
  },

  get: function getWithExpiry(key) {
    const itemStr = localStorage.getItem(key)

    if (!itemStr) {
      return undefined
    }
    const item = JSON.parse(itemStr)
    const now = new Date()

    if (now.getTime() > item.expiry) {
      localStorage.removeItem(key)
      return undefined
    }
    return item.value
  }
}

// https://stackoverflow.com/questions/16839698/jquery-getscript-alternative-in-native-javascript
const getScript = url => new Promise((resolve, reject) => {
  const script = document.createElement('script')
  script.src = url
  script.async = true
  script.onerror = reject
  script.onload = script.onreadystatechange = function() {
    const loadState = this.readyState
    if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
    script.onload = script.onreadystatechange = null
    resolve()
  }
  document.head.appendChild(script)
})</script><script id="config_change">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-03-23 09:16:13'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(function () {  window.activateDarkMode = function () {
    document.documentElement.setAttribute('data-theme', 'dark')
    if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
    }
  }
  window.activateLightMode = function () {
    document.documentElement.setAttribute('data-theme', 'light')
   if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
    }
  }
  const autoChangeMode = 'false'
  const t = saveToLocal.get('theme')
  if (autoChangeMode === '1') {
    const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
    const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
    const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
    const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
    if (t === undefined) {
      if (isLightMode) activateLightMode()
      else if (isDarkMode) activateDarkMode()
      else if (isNotSpecified || hasNoSupport) {
        const now = new Date()
        const hour = now.getHours()
        const isNight = hour <= 6 || hour >= 18
        isNight ? activateDarkMode() : activateLightMode()
      }
      window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
        if (saveToLocal.get('theme') === undefined) {
          e.matches ? activateDarkMode() : activateLightMode()
        }
      })
    } else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else if (autoChangeMode === '2') {
    const now = new Date()
    const hour = now.getHours()
    const isNight = hour <= 6 || hour >= 18
    if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
    else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else {
    if (t === 'dark') activateDarkMode()
    else if (t === 'light') activateLightMode()
  }const asideStatus = saveToLocal.get('aside-status')
if (asideStatus !== undefined) {
   if (asideStatus === 'hide') {
     document.documentElement.classList.add('hide-aside')
   } else {
     document.documentElement.classList.remove('hide-aside')
   }
}})()</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><div class="aplayer" data-id="6583040939" data-server="netease" data-type="playlist" data-fixed="true" data-listFolded="false" data-order="random" data-preload="auto"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/lete114/CDN/music/APlayer/APlayer.min.css"><script src="https://cdn.jsdelivr.net/gh/lete114/CDN/music/APlayer/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/meting@1.2.0/dist/Meting.min.js"></script><meta name="generator" content="Hexo 5.3.0"><link rel="alternate" href="/blogs/atom.xml" title="欢迎来到小张的博客" type="application/atom+xml">
</head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="/blogs/img/%E5%8D%9A%E5%AE%A2%E5%A4%B4%E5%83%8F.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/blogs/archives/"><div class="headline">文章</div><div class="length-num">29</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/blogs/tags/"><div class="headline">标签</div><div class="length-num">12</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/blogs/categories/"><div class="headline">分类</div><div class="length-num">12</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blogs/"><i class="fa-fw fa fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/link/"><i class="fa-fw fa fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/about/"><i class="fa-fw fa fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://i.loli.net/2021/03/17/7x6uAiswPjbJknS.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blogs/">欢迎来到小张的博客</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blogs/"><i class="fa-fw fa fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/link/"><i class="fa-fw fa fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/about/"><i class="fa-fw fa fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">MongoDB安装及使用</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="fa-fw post-meta-icon far fa-calendar-alt"></i><span class="post-meta-label">发表于</span><time datetime="2019-03-18T11:16:58.000Z" title="undefined 2019-03-18 19:16:58">2019-03-18</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blogs/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="MongoDB安装"><a href="#MongoDB安装" class="headerlink" title="MongoDB安装"></a>MongoDB安装</h2><h3 id="MongoDB安装-1"><a href="#MongoDB安装-1" class="headerlink" title="MongoDB安装"></a>MongoDB安装</h3><pre><code>将MongoDB服务下载下来 安装到自己的电脑</code></pre>
<h3 id="MongoDB服务下载并安装"><a href="#MongoDB服务下载并安装" class="headerlink" title="MongoDB服务下载并安装"></a>MongoDB服务下载并安装</h3><ul>
<li>进入<a target="_blank" rel="noopener" href="https://www.mongodb.com/try/download/community">https://www.mongodb.com/try/download/community</a>  这个链接之下，会看到如下界面（一定要选择windows）</li>
</ul>
<p><img src="https://i.loli.net/2021/03/17/VqUOvjn3hWi8dxM.png" alt="MongoDB安装1.png"></p>
<ul>
<li><p>下载完成后，会得到一个后缀为.msi的文件，双击进行安装即可<br><img src="https://i.loli.net/2021/03/17/7YIFDH4BtgESPh8.png" alt="MongoDB安装2.png"></p>
<ul>
<li><h4 id="以下是安装图解（要注意，除以下安装图解之外，全是next）："><a href="#以下是安装图解（要注意，除以下安装图解之外，全是next）：" class="headerlink" title="以下是安装图解（要注意，除以下安装图解之外，全是next）："></a>以下是安装图解（要注意，除以下安装图解之外，全是next）：</h4><p><img src="https://i.loli.net/2021/03/17/hsjJp2PZa9B783l.png" alt="MongoDB安装3.png"></p>
</li>
<li><h4 id="此时我安装的位置是默认磁盘C盘，你也可以选择你要安装的盘符路径"><a href="#此时我安装的位置是默认磁盘C盘，你也可以选择你要安装的盘符路径" class="headerlink" title="此时我安装的位置是默认磁盘C盘，你也可以选择你要安装的盘符路径"></a>此时我安装的位置是默认磁盘C盘，你也可以选择你要安装的盘符路径</h4><p><img src="https://i.loli.net/2021/03/17/gY8vIib6pohUPRB.png" alt="MongoDB安装4.png"></p>
</li>
<li><h4 id="下图一定要取消安装"><a href="#下图一定要取消安装" class="headerlink" title="下图一定要取消安装"></a>下图一定要取消安装</h4><p><img src="https://i.loli.net/2021/03/17/OpjMstBou9Zy4aQ.png" alt="MongoDB安装6.png"></p>
</li>
<li><h4 id="点击next，安装完成，会得到一个安装目录文件夹"><a href="#点击next，安装完成，会得到一个安装目录文件夹" class="headerlink" title="点击next，安装完成，会得到一个安装目录文件夹"></a>点击next，安装完成，会得到一个安装目录文件夹</h4><p><img src="https://i.loli.net/2021/03/17/JBphbwWZ6P8xkuD.png" alt="MongoDB安装5.png"></p>
</li>
<li><h4 id="此时我们需要在安装目录的data目录下创建一个db文件夹，然后使用命令行进入bin目录下，执行mongod-–dbpath-db文件夹位置（例-mongod-–dbpath-C-MongoDB-data-db"><a href="#此时我们需要在安装目录的data目录下创建一个db文件夹，然后使用命令行进入bin目录下，执行mongod-–dbpath-db文件夹位置（例-mongod-–dbpath-C-MongoDB-data-db" class="headerlink" title="此时我们需要在安装目录的data目录下创建一个db文件夹，然后使用命令行进入bin目录下，执行mongod –dbpath db文件夹位置（例: mongod –dbpath C:\MongoDB\data\db)"></a>此时我们需要在安装目录的data目录下创建一个db文件夹，然后使用命令行进入bin目录下，执行mongod –dbpath db文件夹位置（例: mongod –dbpath C:\MongoDB\data\db)</h4><p><img src="https://i.loli.net/2021/03/17/dLJwXAp31Temo7b.png" alt="MongoDB安装7.png"></p>
</li>
<li><h4 id="在bin目录下打开另一个命令行，执行mongo"><a href="#在bin目录下打开另一个命令行，执行mongo" class="headerlink" title="在bin目录下打开另一个命令行，执行mongo"></a>在bin目录下打开另一个命令行，执行mongo</h4><p><img src="https://i.loli.net/2021/03/17/GFSCHnDx9NRiAMg.png" alt="MongoDB安装8.png"></p>
</li>
<li><h4 id="上图所示，安装MongoDB服务已经成功，还可以直接打开bin目录下的mongo-exe程序，如显示下图，则为成功"><a href="#上图所示，安装MongoDB服务已经成功，还可以直接打开bin目录下的mongo-exe程序，如显示下图，则为成功" class="headerlink" title="上图所示，安装MongoDB服务已经成功，还可以直接打开bin目录下的mongo.exe程序，如显示下图，则为成功"></a>上图所示，安装MongoDB服务已经成功，还可以直接打开bin目录下的mongo.exe程序，如显示下图，则为成功</h4><p><img src="https://i.loli.net/2021/03/17/qSB4RmraD6scyjZ.png" alt="MongoDB安装9.png"></p>
<p><code>如上所示，MongoDB服务已经成功下载并安装</code></p>
</li>
</ul>
</li>
</ul>
<h3 id="MongoDB-Compass（可视化工具）安装"><a href="#MongoDB-Compass（可视化工具）安装" class="headerlink" title="MongoDB Compass（可视化工具）安装"></a>MongoDB Compass（可视化工具）安装</h3><ul>
<li><p>进入 <a target="_blank" rel="noopener" href="https://www.mongodb.com/try/download/compass">https://www.mongodb.com/try/download/compass</a> 这个链接之下，会看到如下界面，进行下载即可<br><img src="https://i.loli.net/2021/03/17/swB2vmuCDMrJTLU.png" alt="MongoDB Compass安装1.png"></p>
</li>
<li><p>下载完成后，会得到一个压缩文件，解压到任意文件夹里即可（我是在D盘创建了一个MongoDB-compass文件夹，进行解压）<br><img src="https://i.loli.net/2021/03/17/R6aVmKYBUpActqu.png" alt="MongoDB Compass安装2.png"></p>
</li>
<li><h4 id="如上图所示，压缩包解压之后，上方有一个MongoDBCompass-exe文件，打开即可"><a href="#如上图所示，压缩包解压之后，上方有一个MongoDBCompass-exe文件，打开即可" class="headerlink" title="如上图所示，压缩包解压之后，上方有一个MongoDBCompass.exe文件，打开即可"></a>如上图所示，压缩包解压之后，上方有一个MongoDBCompass.exe文件，打开即可</h4><p><img src="https://i.loli.net/2021/03/17/XEOK4pVlhJWkIe6.png" alt="MongoDB Compass安装3.png"></p>
</li>
<li><h4 id="点击Connect进行连接"><a href="#点击Connect进行连接" class="headerlink" title="点击Connect进行连接"></a>点击Connect进行连接</h4><p><img src="https://i.loli.net/2021/03/17/3S8GZMJuQUqEslz.png" alt="MongoDB Compass安装4.png"></p>
</li>
</ul>
<p><code>如上所示，MongoDB-Compass 可视化工具安装成功</code></p>
<h2 id="MongoDB使用"><a href="#MongoDB使用" class="headerlink" title="MongoDB使用"></a>MongoDB使用</h2><h3 id="基本语法"><a href="#基本语法" class="headerlink" title="基本语法"></a>基本语法</h3><table>
    <tr>
        <th>命令</th>
        <th>语法</th>
    </tr>
    <tr>
        <td>展示所有数据库</td>
        <td>show dbs</td>
    </tr>
    <tr>
        <td>创建数据库</td>
        <td>use 数据库名</td>
    </tr>
    <tr>
        <td>查看当前所在数据库</td>
        <td>db</td>
    </tr>
    <tr>
        <td>创建集合</td>
        <td>db.createCollection('名称')</td>
    </tr>
    <tr>
        <td>展示集合</td>
        <td>show 集合名</td>
    </tr>
    <tr>
        <td>插入数据</td>
        <td>db.集合.insert({...})</td>
    </tr>
    <tr>
        <td>读取数据</td>
        <td>db.集合.find()</td>
    </tr>
    <tr>
        <td>读取指定数据</td>
        <td>db.集合.find({id:1})</td>
    </tr>
</table>

<h3 id="python操作MongoDB"><a href="#python操作MongoDB" class="headerlink" title="python操作MongoDB"></a>python操作MongoDB</h3><ul>
<li><h4 id="下载python中的MongoDB"><a href="#下载python中的MongoDB" class="headerlink" title="下载python中的MongoDB"></a>下载python中的MongoDB</h4><blockquote>
<p>pip install pymongo</p>
</blockquote>
</li>
<li><h4 id="下载完成之后，进行连接（此处要导包）"><a href="#下载完成之后，进行连接（此处要导包）" class="headerlink" title="下载完成之后，进行连接（此处要导包）"></a>下载完成之后，进行连接（此处要导包）</h4><blockquote>
<p>from pymongo import MongoClient<br>client = MongoClient(host=’127.0.0.1’, port=27017)</p>
</blockquote>
</li>
<li><h4 id="接下来需要进入我们在MongoDB创建的数据库（我创建的是z-Cate）"><a href="#接下来需要进入我们在MongoDB创建的数据库（我创建的是z-Cate）" class="headerlink" title="接下来需要进入我们在MongoDB创建的数据库（我创建的是z_Cate）"></a>接下来需要进入我们在MongoDB创建的数据库（我创建的是z_Cate）</h4><code>此处没有创建数据库的，需要手动创建MongoDB数据库（看上方的基本语法）</code><blockquote>
<p>db = client.z_Cate</p>
</blockquote>
</li>
<li><h4 id="指定集合"><a href="#指定集合" class="headerlink" title="指定集合"></a>指定集合</h4><blockquote>
<p>table = db.CateTemplate</p>
</blockquote>
</li>
<li><h4 id="建立好之后，就可以进行查询了"><a href="#建立好之后，就可以进行查询了" class="headerlink" title="建立好之后，就可以进行查询了"></a>建立好之后，就可以进行查询了</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">get</span>(<span class="params">self, request</span>):</span></span><br><span class="line">    <span class="comment"># 进入数据库</span></span><br><span class="line">    db = client.z_Cate</span><br><span class="line">    <span class="comment"># 集合</span></span><br><span class="line">    zst = db.CateTemplate</span><br><span class="line">    <span class="built_in">id</span> = request.GET.get(<span class="string">&#x27;id&#x27;</span>, <span class="literal">None</span>)</span><br><span class="line">    wid = WorkOrderModel.objects.<span class="built_in">filter</span>(<span class="built_in">id</span>=<span class="built_in">id</span>).first()</span><br><span class="line">    data = &#123;&#125;</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> zst.find(&#123;<span class="string">&#x27;wid&#x27;</span>: <span class="built_in">str</span>(wid.pid)&#125;):</span><br><span class="line">        data = <span class="built_in">eval</span>(i[<span class="string">&#x27;name&#x27;</span>])</span><br><span class="line">    <span class="keyword">return</span> Response(data)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<hr>
<p><code>想要查看更多知识，请看以下链接</code><br>Python操作MongoDB：<a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python-mongodb.html">https://www.runoob.com/python3/python-mongodb.html</a><br>MongoDB介绍：<a target="_blank" rel="noopener" href="https://www.runoob.com/mongodb/mongodb-tutorial.html">https://www.runoob.com/mongodb/mongodb-tutorial.html</a></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Z_SiTong</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://zhang-sitong.gitee.io/blogs/lz_17/">https://zhang-sitong.gitee.io/blogs/lz_17/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://zhang-sitong.gitee.io/blogs" target="_blank">欢迎来到小张的博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blogs/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a></div><div class="post_share"><div class="social-share" data-image="https://i.loli.net/2021/03/17/7x6uAiswPjbJknS.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blogs/lz_18/"><img class="prev-cover" src="https://i.loli.net/2021/03/21/BNz2vCwnVTsXHrf.jpg" onerror="onerror=null;src='/blogs/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">MySQL和NoSQL</div></div></a></div><div class="next-post pull-right"><a href="/blogs/lz_16/"><img class="next-cover" src="https://i.loli.net/2021/03/23/KM1mN2wtGXSukBd.jpg" onerror="onerror=null;src='/blogs/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">上下文管理</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/blogs/lz_18/" title="MySQL和NoSQL"><img class="cover" src="https://i.loli.net/2021/03/21/BNz2vCwnVTsXHrf.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-21</div><div class="title">MySQL和NoSQL</div></div></a></div><div><a href="/blogs/lz_24/" title="openLDAP"><img class="cover" src="https://i.loli.net/2021/04/07/nLeJybVMHwFNj7G.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-31</div><div class="title">openLDAP</div></div></a></div><div><a href="/blogs/lz_27/" title="基于Docker安装关系型数据库PostgrelSQL替代Mysql"><img class="cover" src="https://i.loli.net/2021/04/19/QRXbxEvnLpBHuzI.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-04-14</div><div class="title">基于Docker安装关系型数据库PostgrelSQL替代Mysql</div></div></a></div><div><a href="/blogs/lz_8/" title="redis发布者订阅者"><img class="cover" src="https://i.loli.net/2021/02/01/H5oBbvFCQUTEsyW.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-02-01</div><div class="title">redis发布者订阅者</div></div></a></div><div><a href="/blogs/lz_9/" title="redis键空间事件"><img class="cover" src="https://i.loli.net/2021/02/02/bGkq4nN38tABerD.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-02-02</div><div class="title">redis键空间事件</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="/blogs/img/%E5%8D%9A%E5%AE%A2%E5%A4%B4%E5%83%8F.jpg" onerror="this.onerror=null;this.src='/blogs/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">Z_SiTong</div><div class="author-info__description"></div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/blogs/archives/"><div class="headline">文章</div><div class="length-num">29</div></a></div><div class="card-info-data-item is-center"><a href="/blogs/tags/"><div class="headline">标签</div><div class="length-num">12</div></a></div><div class="card-info-data-item is-center"><a href="/blogs/categories/"><div class="headline">分类</div><div class="length-num">12</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">This is my blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#MongoDB%E5%AE%89%E8%A3%85"><span class="toc-number">1.</span> <span class="toc-text">MongoDB安装</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#MongoDB%E5%AE%89%E8%A3%85-1"><span class="toc-number">1.1.</span> <span class="toc-text">MongoDB安装</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#MongoDB%E6%9C%8D%E5%8A%A1%E4%B8%8B%E8%BD%BD%E5%B9%B6%E5%AE%89%E8%A3%85"><span class="toc-number">1.2.</span> <span class="toc-text">MongoDB服务下载并安装</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BB%A5%E4%B8%8B%E6%98%AF%E5%AE%89%E8%A3%85%E5%9B%BE%E8%A7%A3%EF%BC%88%E8%A6%81%E6%B3%A8%E6%84%8F%EF%BC%8C%E9%99%A4%E4%BB%A5%E4%B8%8B%E5%AE%89%E8%A3%85%E5%9B%BE%E8%A7%A3%E4%B9%8B%E5%A4%96%EF%BC%8C%E5%85%A8%E6%98%AFnext%EF%BC%89%EF%BC%9A"><span class="toc-number">1.2.1.</span> <span class="toc-text">以下是安装图解（要注意，除以下安装图解之外，全是next）：</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%AD%A4%E6%97%B6%E6%88%91%E5%AE%89%E8%A3%85%E7%9A%84%E4%BD%8D%E7%BD%AE%E6%98%AF%E9%BB%98%E8%AE%A4%E7%A3%81%E7%9B%98C%E7%9B%98%EF%BC%8C%E4%BD%A0%E4%B9%9F%E5%8F%AF%E4%BB%A5%E9%80%89%E6%8B%A9%E4%BD%A0%E8%A6%81%E5%AE%89%E8%A3%85%E7%9A%84%E7%9B%98%E7%AC%A6%E8%B7%AF%E5%BE%84"><span class="toc-number">1.2.2.</span> <span class="toc-text">此时我安装的位置是默认磁盘C盘，你也可以选择你要安装的盘符路径</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%8B%E5%9B%BE%E4%B8%80%E5%AE%9A%E8%A6%81%E5%8F%96%E6%B6%88%E5%AE%89%E8%A3%85"><span class="toc-number">1.2.3.</span> <span class="toc-text">下图一定要取消安装</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%82%B9%E5%87%BBnext%EF%BC%8C%E5%AE%89%E8%A3%85%E5%AE%8C%E6%88%90%EF%BC%8C%E4%BC%9A%E5%BE%97%E5%88%B0%E4%B8%80%E4%B8%AA%E5%AE%89%E8%A3%85%E7%9B%AE%E5%BD%95%E6%96%87%E4%BB%B6%E5%A4%B9"><span class="toc-number">1.2.4.</span> <span class="toc-text">点击next，安装完成，会得到一个安装目录文件夹</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%AD%A4%E6%97%B6%E6%88%91%E4%BB%AC%E9%9C%80%E8%A6%81%E5%9C%A8%E5%AE%89%E8%A3%85%E7%9B%AE%E5%BD%95%E7%9A%84data%E7%9B%AE%E5%BD%95%E4%B8%8B%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AAdb%E6%96%87%E4%BB%B6%E5%A4%B9%EF%BC%8C%E7%84%B6%E5%90%8E%E4%BD%BF%E7%94%A8%E5%91%BD%E4%BB%A4%E8%A1%8C%E8%BF%9B%E5%85%A5bin%E7%9B%AE%E5%BD%95%E4%B8%8B%EF%BC%8C%E6%89%A7%E8%A1%8Cmongod-%E2%80%93dbpath-db%E6%96%87%E4%BB%B6%E5%A4%B9%E4%BD%8D%E7%BD%AE%EF%BC%88%E4%BE%8B-mongod-%E2%80%93dbpath-C-MongoDB-data-db"><span class="toc-number">1.2.5.</span> <span class="toc-text">此时我们需要在安装目录的data目录下创建一个db文件夹，然后使用命令行进入bin目录下，执行mongod –dbpath db文件夹位置（例: mongod –dbpath C:\MongoDB\data\db)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9C%A8bin%E7%9B%AE%E5%BD%95%E4%B8%8B%E6%89%93%E5%BC%80%E5%8F%A6%E4%B8%80%E4%B8%AA%E5%91%BD%E4%BB%A4%E8%A1%8C%EF%BC%8C%E6%89%A7%E8%A1%8Cmongo"><span class="toc-number">1.2.6.</span> <span class="toc-text">在bin目录下打开另一个命令行，执行mongo</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%8A%E5%9B%BE%E6%89%80%E7%A4%BA%EF%BC%8C%E5%AE%89%E8%A3%85MongoDB%E6%9C%8D%E5%8A%A1%E5%B7%B2%E7%BB%8F%E6%88%90%E5%8A%9F%EF%BC%8C%E8%BF%98%E5%8F%AF%E4%BB%A5%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80bin%E7%9B%AE%E5%BD%95%E4%B8%8B%E7%9A%84mongo-exe%E7%A8%8B%E5%BA%8F%EF%BC%8C%E5%A6%82%E6%98%BE%E7%A4%BA%E4%B8%8B%E5%9B%BE%EF%BC%8C%E5%88%99%E4%B8%BA%E6%88%90%E5%8A%9F"><span class="toc-number">1.2.7.</span> <span class="toc-text">上图所示，安装MongoDB服务已经成功，还可以直接打开bin目录下的mongo.exe程序，如显示下图，则为成功</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#MongoDB-Compass%EF%BC%88%E5%8F%AF%E8%A7%86%E5%8C%96%E5%B7%A5%E5%85%B7%EF%BC%89%E5%AE%89%E8%A3%85"><span class="toc-number">1.3.</span> <span class="toc-text">MongoDB Compass（可视化工具）安装</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%A6%82%E4%B8%8A%E5%9B%BE%E6%89%80%E7%A4%BA%EF%BC%8C%E5%8E%8B%E7%BC%A9%E5%8C%85%E8%A7%A3%E5%8E%8B%E4%B9%8B%E5%90%8E%EF%BC%8C%E4%B8%8A%E6%96%B9%E6%9C%89%E4%B8%80%E4%B8%AAMongoDBCompass-exe%E6%96%87%E4%BB%B6%EF%BC%8C%E6%89%93%E5%BC%80%E5%8D%B3%E5%8F%AF"><span class="toc-number">1.3.1.</span> <span class="toc-text">如上图所示，压缩包解压之后，上方有一个MongoDBCompass.exe文件，打开即可</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%82%B9%E5%87%BBConnect%E8%BF%9B%E8%A1%8C%E8%BF%9E%E6%8E%A5"><span class="toc-number">1.3.2.</span> <span class="toc-text">点击Connect进行连接</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#MongoDB%E4%BD%BF%E7%94%A8"><span class="toc-number">2.</span> <span class="toc-text">MongoDB使用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95"><span class="toc-number">2.1.</span> <span class="toc-text">基本语法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#python%E6%93%8D%E4%BD%9CMongoDB"><span class="toc-number">2.2.</span> <span class="toc-text">python操作MongoDB</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%8B%E8%BD%BDpython%E4%B8%AD%E7%9A%84MongoDB"><span class="toc-number">2.2.1.</span> <span class="toc-text">下载python中的MongoDB</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%8B%E8%BD%BD%E5%AE%8C%E6%88%90%E4%B9%8B%E5%90%8E%EF%BC%8C%E8%BF%9B%E8%A1%8C%E8%BF%9E%E6%8E%A5%EF%BC%88%E6%AD%A4%E5%A4%84%E8%A6%81%E5%AF%BC%E5%8C%85%EF%BC%89"><span class="toc-number">2.2.2.</span> <span class="toc-text">下载完成之后，进行连接（此处要导包）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8E%A5%E4%B8%8B%E6%9D%A5%E9%9C%80%E8%A6%81%E8%BF%9B%E5%85%A5%E6%88%91%E4%BB%AC%E5%9C%A8MongoDB%E5%88%9B%E5%BB%BA%E7%9A%84%E6%95%B0%E6%8D%AE%E5%BA%93%EF%BC%88%E6%88%91%E5%88%9B%E5%BB%BA%E7%9A%84%E6%98%AFz-Cate%EF%BC%89"><span class="toc-number">2.2.3.</span> <span class="toc-text">接下来需要进入我们在MongoDB创建的数据库（我创建的是z_Cate）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8C%87%E5%AE%9A%E9%9B%86%E5%90%88"><span class="toc-number">2.2.4.</span> <span class="toc-text">指定集合</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BB%BA%E7%AB%8B%E5%A5%BD%E4%B9%8B%E5%90%8E%EF%BC%8C%E5%B0%B1%E5%8F%AF%E4%BB%A5%E8%BF%9B%E8%A1%8C%E6%9F%A5%E8%AF%A2%E4%BA%86"><span class="toc-number">2.2.5.</span> <span class="toc-text">建立好之后，就可以进行查询了</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blogs/lz_28/" title="多因子登录基础知识点"><img src="https://i.loli.net/2021/04/19/T9cDNdmovulPEbL.jpg" onerror="this.onerror=null;this.src='/blogs/img/404.jpg'" alt="多因子登录基础知识点"/></a><div class="content"><a class="title" href="/blogs/lz_28/" title="多因子登录基础知识点">多因子登录基础知识点</a><time datetime="2019-04-19T12:08:59.000Z" title="发表于 2019-04-19 20:08:59">2019-04-19</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blogs/lz_27/" title="基于Docker安装关系型数据库PostgrelSQL替代Mysql"><img src="https://i.loli.net/2021/04/19/QRXbxEvnLpBHuzI.jpg" onerror="this.onerror=null;this.src='/blogs/img/404.jpg'" alt="基于Docker安装关系型数据库PostgrelSQL替代Mysql"/></a><div class="content"><a class="title" href="/blogs/lz_27/" title="基于Docker安装关系型数据库PostgrelSQL替代Mysql">基于Docker安装关系型数据库PostgrelSQL替代Mysql</a><time datetime="2019-04-14T01:42:58.000Z" title="发表于 2019-04-14 09:42:58">2019-04-14</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blogs/lz_26/" title="json与pickle"><img src="https://i.loli.net/2021/04/13/DtnL4OVQM7bZjId.jpg" onerror="this.onerror=null;this.src='/blogs/img/404.jpg'" alt="json与pickle"/></a><div class="content"><a class="title" href="/blogs/lz_26/" title="json与pickle">json与pickle</a><time datetime="2019-04-12T07:48:41.000Z" title="发表于 2019-04-12 15:48:41">2019-04-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blogs/lz_25/" title="集合(set)的用法和使用场景，以及底层去重算法"><img src="https://i.loli.net/2021/04/08/Ji5dTUKsnoGjRBz.jpg" onerror="this.onerror=null;this.src='/blogs/img/404.jpg'" alt="集合(set)的用法和使用场景，以及底层去重算法"/></a><div class="content"><a class="title" href="/blogs/lz_25/" title="集合(set)的用法和使用场景，以及底层去重算法">集合(set)的用法和使用场景，以及底层去重算法</a><time datetime="2019-04-06T02:48:41.000Z" title="发表于 2019-04-06 10:48:41">2019-04-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blogs/lz_24/" title="openLDAP"><img src="https://i.loli.net/2021/04/07/nLeJybVMHwFNj7G.jpg" onerror="this.onerror=null;this.src='/blogs/img/404.jpg'" alt="openLDAP"/></a><div class="content"><a class="title" href="/blogs/lz_24/" title="openLDAP">openLDAP</a><time datetime="2019-03-31T08:52:13.000Z" title="发表于 2019-03-31 16:52:13">2019-03-31</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2021 <i style="color:#FF6A6A;animation: announ_animation 0.8s linear infinite;" class="fa fa-heartbeat"></i> Z_SiTong</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/blogs/js/utils.js"></script><script src="/blogs/js/main.js"></script><script src="/blogs/js/search/local-search.js"></script><div class="js-pjax"></div><script src="https://cdn.jsdelivr.net/gh/lete114/CDN/Sum/sakura.js"></script><script src="https://cdn.jsdelivr.net/gh/lete114/CDN/Sum/title.js"></script>null<script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>